class Solution {
public:
    string decodeString(string s) {
        int currentNum=0;
        string currentStr;
        stack<int> numStack;
        stack<string> strStack;

        for(char c:s){
            if(c>='0'&&c<='9'){
                currentNum=currentNum*10+(c-'0');
            }
            else if(c>='a'&&c<='z'){
                currentStr+=c;
            }
            else if(c=='['){
                numStack.push(currentNum);
                strStack.push(currentStr);
                currentNum=0;
                currentStr="";
            }
            else if(c==']'){
                int k=numStack.top();
                numStack.pop();
                string prevStr=strStack.top();
                strStack.pop();
                string temp;
                for(int i=0;i<k;i++){
                    temp+=currentStr;
                }
                currentStr=prevStr+temp;
            }
        }
        return currentStr;

    }
};